package com.dmz.wxcp.wecom.wxbusiness.controller;


import com.dmz.wxcp.utils.DataResult;
import com.dmz.wxcp.vo.req.ProductReqVO;
import com.dmz.wxcp.vo.resp.PageRespVO;
import com.dmz.wxcp.wecom.wxbusiness.entity.Article;
import com.dmz.wxcp.wecom.wxbusiness.mapper.ProductMapper;
import com.dmz.wxcp.wecom.wxbusiness.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/product")
public class ProductController {



    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductService productService;



    @RequestMapping("/getproduct")
    @ResponseBody
    public DataResult productlist(@RequestParam Map<String,Object> map){
        ProductReqVO vo=new ProductReqVO();
        vo.setTag((map.get("tag").toString()));
        vo.setProductName(map.get("productname").toString());
        vo.setCurrentPage(new Long(map.get("page").toString()));
        vo.setPageSize(new Long(map.get("limit").toString()));
        vo.setStartNum((vo.getCurrentPage()-1)*vo.getPageSize());

        List<Object> result=productMapper.getProductList(vo);
        List<Article> articles=(List<Article>)result.get(0);
        Long count= new Long(((List<Integer>) result.get(1)).get(0));

        PageRespVO<Article> respVO=new PageRespVO<Article>();
        respVO.setRecords(articles);
        respVO.setTotal(count);
//        获取页数  总数除以每页数量
        Long size=count % vo.getPageSize() == 0 ? count/vo.getPageSize() :count/vo.getPageSize()+1;
        respVO.setSize(size);
        return DataResult.success(respVO);
    }


    @GetMapping("/productindex")
    public String productindex(Model model){
        Map<String,Object> productCategoryList=productService.getTags();
        model.addAttribute("tags",productCategoryList);
        return "productindex";
    }


}
